package com.jlnk.stockdemo.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jlnk.stockdemo.R;
import com.jlnk.stockdemo.adapter.ForexAdapter;
import com.jlnk.stockdemo.bean.ForexListResponse;
import com.jlnk.stockdemo.viewmodel.ForexViewModel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class ForexFragment extends Fragment {
    private static final String TAG = "ForexFragment";
    private ForexViewModel forexViewModel;
    private RecyclerView recyclerview_forex;
    private ForexAdapter forexAdapter;
    private List<ForexListResponse.Data.Forex> forexList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_forex, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        forexAdapter = new ForexAdapter(forexList);
        recyclerview_forex = view.findViewById(R.id.recyclerview_forex);
        recyclerview_forex.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerview_forex.setAdapter(forexAdapter);

        forexViewModel = new ViewModelProvider(this).get(ForexViewModel.class);
        forexViewModel.getForexListLiveData().observe(getViewLifecycleOwner(), forexs -> {
            this.forexList.clear();
            this.forexList.addAll(forexs);
            forexAdapter.notifyDataSetChanged();
        });
        forexViewModel.fetchForexList();
    }
}
